package com.example.its2021606134chh.UserModule;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.its2021606134chh.LoginModule.params;
import com.example.its2021606134chh.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserListViewAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 6/9/2021$
 * @Version: 1.0
 */
public class UserListViewAdapter extends BaseAdapter {
    Context context;
    List<UserData.ROWSDETAILBean> list;
    List<params> userList;
    LayoutInflater inflater;
//    private LayoutParams textViewParams;
//    private LayoutParams buttonParams;

//    public UserListViewAdapter(Context context, List<UserData.ROWSDETAILBean> list, List<params> userList, LayoutParams textViewParams, LayoutParams buttonParams) {
public UserListViewAdapter(Context context, List<UserData.ROWSDETAILBean> list, List<params> userList) {
        this.context = context;
        this.list = list;
        this.userList = userList;
        inflater = LayoutInflater.from(context);
//        this.textViewParams = textViewParams;
//        this.buttonParams = buttonParams;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.itemuser, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        viewHolder.userItemLayout.setLayoutParams(textViewParams);
//        viewHolder.userImgItemLayout.setLayoutParams(buttonParams);
        UserData.ROWSDETAILBean temp = list.get(position);
        if (temp.getPsex().equals("男")) {
            viewHolder.circleImageView.setImageResource(R.drawable.touxiang_1);
        } else {
            viewHolder.circleImageView.setImageResource(R.drawable.touxiang_2);
        }
        viewHolder.tvUserUserName.setText(temp.getPname());
        viewHolder.tvUserUser.setText(temp.getUsername());
        viewHolder.tvUserPhone.setText(temp.getPtel());
        Map<String, String> map = new HashMap<>();
        map.put("R01", "    普通用户");
        map.put("R02", "一般管理员");
        map.put("R03", "超级管理员");
        viewHolder.tvUserType.setText(map.get(userList.get(position).getUserRole()));
        viewHolder.tvUserDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                userList.remove(position);
                notifyDataSetChanged();
            }
        });
        viewHolder.tvUserSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });
        return convertView;
    }

    static class ViewHolder {
        de.hdodenhof.circleimageview.CircleImageView circleImageView;
        TextView tvUserUserName;
        TextView tvUserUser;
        TextView tvUserPhone;
        TextView tvUserType;
        TextView tvUserSave;
        TextView tvUserDelete;
        LinearLayout userItemLayout;
        LinearLayout userImgItemLayout;
        public ViewHolder (View view) {
            circleImageView = view.findViewById(R.id.circleImageView);
            tvUserUserName = view.findViewById(R.id.tvUserUserName);
            tvUserUser = view.findViewById(R.id.tvUserUser);
            tvUserPhone = view.findViewById(R.id.tvUserPhone);
            tvUserType = view.findViewById(R.id.tvUserType);
            tvUserSave = view.findViewById(R.id.tvUserSave);
            tvUserDelete = view.findViewById(R.id.tvUserDelete);
            userItemLayout = view.findViewById(R.id.userItemLayout);
            userImgItemLayout = view.findViewById(R.id.userImgItemLayout);
        }
    }
}
